<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
     
      <el-form-item label="讲师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入讲师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['curriculum:teacher:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['curriculum:teacher:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['curriculum:teacher:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['curriculum:teacher:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" :index="createIndex" />
      <el-table-column label="讲师照片" align="center" prop="avatar" >
                <!-- 图片的显示 -->
                <template slot-scope="scope">            
            <img :src="scope.row.avatar"  min-width="70px" height="70px" 
            @click="previewPic(scope.row.avatar)" />
        </template>     
      </el-table-column>
      <el-table-column label="讲师名称" align="center" prop="teacherName">
      </el-table-column>
      <el-table-column label="讲师介绍" align="center" prop="teacherInfo">
        <template slot-scope="scope">
          <span @click="viewName(scope.row)">讲师介绍</span>
        </template>
        </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch  v-model="scope.row.status"
                :active-value="0"
                :inactive-value="1"
                @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['curriculum:teacher:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['curriculum:teacher:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

        <!-- 图片预览   放在表格外面，这是个弹出框-->
        <el-dialog :visible.sync="dialogVisible" :modal="false" title="图片预览" width="50%">
        <img :src="previewpic" alt=""  width="100%" />
        <span slot="footer" class="dialog-footer">
          </span>
       </el-dialog>
  <!-- 图片预览 -->

    <el-dialog :visible.sync="viewOpen" :modal="false" :title="title" width="50%">
   <el-form ref="form" :model="form">
<div style="width: 100%" class="previewWrapper">
	<div class="previewContainer">
		<div class="card" style="padding:10px 10px 10px 10px;text-align:left;display: none" >
		</div>
		<div class="card" style="padding:10px 10px 10px 10px;text-align:center;display: none">
		</div>
		<div class="card" style="text-align:center;display: none">
		</div>
		<div class="card" style="padding:10px 10px 10px 10px;display: none">
		</div>
		<div style="max-height: 667px" class="contentContainer">
			<div style="margin-bottom: 10px">
				<strong style="font-size: 18px;">{{form.teacherName}}</strong>
			</div>
			<span v-html="form.teacherInfo"></span>
		</div>
	</div>
</div>
   </el-form>
 </el-dialog>

    <!-- 添加或修改课程教师对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :lg="12">
        <el-form-item label="讲师名称" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入讲师名称" />
        </el-form-item>
        <el-form-item label="讲师照片" prop="avatar">
          <el-upload
          class="avatar-uploader"
          ref="upload"
          :action="upload.url"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        </el-form-item>
          </el-col>
        <el-col :lg="12">
          <el-form-item label="讲师介绍" prop="teacherInfo">
            <div class="divList">
              <div class="contentContainer">
            <editor v-model="form.teacherInfo" :min-height="350"/>
          </div>
          </div>
        </el-form-item></el-col>
        </el-row>>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher,changeTeacherStatus } from "@/api/curriculum/teacher";
import { getToken } from "@/utils/auth";

export default {
  name: "Teacher",
  data() {
    return {
      dialogVisible: false,
      previewpic: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程教师表格数据
      teacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //上传参数
       upload:{
      // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
      // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/content/press/importData"
      },
       imageUrl: '',
      viewOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        asId: null,
        teacherName: null,
        teacherInfo: null,
        status: null,
        avatar: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    createIndex(index){
      return (this.queryParams.pageNum-1)*(this.queryParams.pageSize)+index+1;
    },
    /** 查询课程教师列表 */
    getList() {
      this.loading = true;
      listTeacher(this.queryParams).then(response => {
        console.info(response);
        this.teacherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
      //查看视频介绍
      viewName(row){
      this.form=row;
      this.viewOpen=true;
      this.title="查看介绍";
    },  
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
          // 状态修改
    handleStatusChange(row) {
      console.info(row.status);
      let text = row.status == 0 ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.teacherName+ '"讲师吗？').then(function() {
        return changeTeacherStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
     //图片放大
     previewPic(url) {
     this.previewpic = url;
    this.dialogVisible = true;
        },
    handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        if(res.code==200){
          this.form.avatar=res.data;
        }else{
          this.$modal.confirm("上传失败")
        }

      },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        asId: null,
        teacherName: null,
        teacherInfo: null,
        status: 0,
        createTime: null,
        updateTime: null,
        avatar: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程教师";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeacher(id).then(response => {
        this.form = response.data;
        this.imageUrl=response.data.avatar;
        this.open = true;
        this.title = "修改课程教师";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeacher(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeacher(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程教师编号为"' + ids + '"的数据项？').then(function() {
        return delTeacher(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('curriculum/teacher/export', {
        ...this.queryParams
      }, `teacher_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.divList{
  height:350px;
}
.scrollbar_wrap{
  height: 100%;
  overflow-x: hidden;
}
.scrollbar_wrap::-webkit-scrollbar {
  display: none;
}
  .previewWrapper{
      display: flex;
      justify-content: center;
    }
  .previewContainer{
      width: 400px !important;
      height: 667px !important;
      background-image: url("https://shequncloud.oss-cn-shenzhen.aliyuncs.com/material/phoneKK.png");
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 85px 35px 86px 35px;
      box-sizing: border-box;
      position: relative;
    }
  .contentContainer{
      height: 100%;
      overflow-y: auto;
      overflow-x: hidden;
    }
  .contentContainer::-webkit-scrollbar {
    display: none;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  </style>
